import time
import threading
from .socket_server import SocketServer;


class FlowServer:
    def __init__(self, buf):
        self.buf = buf
        self._stop = False
        self.socketServer = SocketServer()

        self.thread = threading.Thread(target=self.loop,
                                       name='flow_server')
        self.thread.start()

    def stop(self):
        self._stop = True
        self.socketServer.stop()
        self.thread.join()

    def loop(self):
        while not self._stop:
            try:
                line = self.buf.pop(0)
                self.socketServer.send(line)
            except IndexError as e:
                time.sleep(1)